package com.yupi.yoj.mq;

import cn.hutool.json.JSONUtil;
import com.yupi.yoj.common.ErrorCode;
import com.yupi.yoj.constant.QueueConstant;
import com.yupi.yoj.exception.ThrowUtils;
import com.yupi.yoj.judge.codesandbox.model.ExecuteCodeRequest;
import com.yupi.yoj.judge.codesandbox.service.ExecuteService;
import com.yupi.yoj.model.dto.questionsubmit.QuestionSubmitVO;
import com.yupi.yoj.model.entity.Contest;
import com.yupi.yoj.model.entity.ContestUser;
import com.yupi.yoj.model.entity.JudgeCase;
import com.yupi.yoj.model.vo.QuestionVO;
import com.yupi.yoj.service.ContestUserService;
import com.yupi.yoj.service.QuestionService;
import com.yupi.yoj.service.QuestionSubmitService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.List;

@Component
public class RabbitMQListener {

    @Autowired
    ContestUserService contestUserService;

    @Autowired
    QuestionSubmitService questionSubmitService;

    @Autowired
    QuestionService questionService;

    @Autowired
    ExecuteService executeService;

    @RabbitListener(queues = QueueConstant.QUEUE_NAME)
    public void produceSubmitCode(String message) {
        System.out.println("Received message: 用沙箱执行代码 拿到结果 修改数据库中的提交信息" + message);
        Long submitId = Long.parseLong(message);
        QuestionSubmitVO questionSubmitVOById = questionSubmitService.getQuestionSubmitVOById(submitId);
        ThrowUtils.throwIf(questionSubmitVOById == null, ErrorCode.NOT_FOUND_ERROR, "提交记录不存在");
        String code = questionSubmitVOById.getCode();
        ThrowUtils.throwIf(code==null, ErrorCode.NOT_FOUND_ERROR, "提交代码为空");
        String language = questionSubmitVOById.getLanguage();
        ThrowUtils.throwIf(language==null, ErrorCode.NOT_FOUND_ERROR, "提交语言类型为空");
        Long questionid = questionSubmitVOById.getQuestionid();
        ThrowUtils.throwIf(questionid==null, ErrorCode.NOT_FOUND_ERROR, "未找到对应题目");
        // 查询题目
        QuestionVO questionById = questionService.getQuestionById(questionid);
        ThrowUtils.throwIf(questionById==null, ErrorCode.NOT_FOUND_ERROR, "未找到对应题目");
        JudgeCase judgecase = questionById.getJudgecase();
        ThrowUtils.throwIf(judgecase==null, ErrorCode.PARAMS_ERROR, "题目输入输出示例为空");
        List<String> inputList = judgecase.getInput();
        List<String> outputList = judgecase.getOutput();

        ExecuteCodeRequest executeCodeRequest = new ExecuteCodeRequest();
        executeCodeRequest.setCode(code);
        executeCodeRequest.setLanguage(language);
        executeCodeRequest.setInputList(inputList);
        // 请求沙箱 计算结果
        executeService.executePost("http://localhost:8888/codeSendbox/execute", executeCodeRequest);
        // 用沙箱执行代码 拿到结果 修改数据库中的提交信息
    }
    @RabbitListener(queues = QueueConstant.QUEUE_COMPUTE_CONTEST)
    public void produceContestScore(String message) {
        System.out.println("Received message: 计算竞赛结果，修改数据库" + message);

        Long contestUserId = Long.parseLong(message);

        ContestUser contestUser = contestUserService.getById(contestUserId);
        ThrowUtils.throwIf(contestUser==null, ErrorCode.SYSTEM_ERROR);
//         计算竞赛结果，修改数据库
        contestUserService.calculateContestScore(contestUser);

    }

    @RabbitListener(bindings = @QueueBinding(value=@Queue("direct.queue1"), exchange = @Exchange(name="direct.exchange", type = ExchangeTypes.DIRECT), key={"direct.key1"}))
    public void directQueue1Listen(String in) {
        System.out.println("direct.queue1:Received message: " + in);
    }

    @RabbitListener(bindings = @QueueBinding(value=@Queue("direct.queue2"), exchange = @Exchange(name="direct.exchange", type = ExchangeTypes.DIRECT),  key={"direct.key2"}))
    public void directQueue2listen(String in) {
        System.out.println("direct.queue2:Received message: " + in);
    }
}